package com.supor.aiot.mine.helper;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.mine.bean.ImageBucket;
import com.supor.aiot.mine.bean.ImageItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AlbumHelper {
    private static final String TAG = AlbumHelper.class.getName();

    private static Map<String, ImageBucket> buildImagesBucketList(final Context context) {
        final Map<String, String> thumbnail = getThumbnail(context);
        final HashMap<String, ImageBucket> hashMap = new HashMap<String, ImageBucket>();
        final Cursor query =
                context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new String[]{"_id", "bucket_id",
                                "picasa_id", "_data", "date_added", "_display_name", "title",
                                "_size", "bucket_display_name"},
                        null, null, "date_added DESC");
        if (query == null) {
            Logc.e(AlbumHelper.TAG, "cursor is null");
            return new HashMap<String, ImageBucket>();
        }
        if (query.moveToFirst()) {
            final int id = query.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            final int data = query.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            final int dataAdded = query.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED);
            final int bucket_display_name =
                    query.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            final int bucket_id = query.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_ID);
            do {
                final String imageId = query.getString(id);
                final String imagePath = query.getString(data);
                final long imageDate = query.getLong(dataAdded);
                final String bucketName = query.getString(bucket_display_name);
                final String bucketId = query.getString(bucket_id);
                ImageBucket imageBucket;
                if ((imageBucket = hashMap.get(bucketId)) == null) {
                    imageBucket = new ImageBucket();
                    hashMap.put(bucketId, imageBucket);
                    imageBucket.imageList = new ArrayList<ImageItem>();
                    imageBucket.bucketName = bucketName;
                    imageBucket.bucketId = bucketId;
                }
                ++imageBucket.count;
                final ImageItem imageItem = new ImageItem();
                imageItem.imageId = imageId;
                imageItem.imagePath = imagePath;
                imageItem.imageDate = imageDate;
                imageItem.thumbnailPath = thumbnail.get(imageId);
                imageBucket.imageList.add(imageItem);
            } while (query.moveToNext());
        }
        query.close();
        return hashMap;
    }

    /**
     * 查询存储图片
     *
     * @param context context
     * @return 图片列表
     */
    public static List<ImageItem> getImageList(final Context context) {
        final Map<String, String> thumbnail = getThumbnail(context);
        final ArrayList<ImageItem> list = new ArrayList<ImageItem>();
        final Cursor query =
                context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new String[]{"_id", "picasa_id", "_data", "date_added", "_display_name",
                                "title", "_size"}, null, null, "date_added DESC");
        if (query == null) {
            Logc.e(AlbumHelper.TAG, "cursor is null");
            return list;
        }
        if (query.moveToFirst()) {
            final int id = query.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            final int data = query.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            final int dataAdded = query.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED);
            final int _size = query.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
            do {
                final String imageId = query.getString(id);
                final String imagePath = query.getString(data);
                final long imageDate = query.getLong(dataAdded);
                final long size = query.getLong(_size);
                final ImageItem imageItem = new ImageItem();
                imageItem.imageId = imageId;
                imageItem.imagePath = imagePath;
                imageItem.imageDate = imageDate;
                imageItem.size = size;
                imageItem.thumbnailPath = thumbnail.get(imageId);
                list.add(imageItem);
            } while (query.moveToNext());
        }
        query.close();
        return list;
    }

    /**
     * 查询存储图片
     *
     * @param context context
     * @return 图片列表
     */
    public static List<ImageItem> getVideoList(final Context context) {
        final Map<String, String> thumbnail = getThumbnail(context);
        final ArrayList<ImageItem> list = new ArrayList<ImageItem>();
        final Cursor query =
                context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        new String[]{"_id", "_data", "date_added", "_display_name",
                                "title", "_size"}, null, null, "date_added DESC");
        if (query == null) {
            Logc.e(AlbumHelper.TAG, "cursor is null");
            return list;
        }
        if (query.moveToFirst()) {
            final int id = query.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
            final int data = query.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            final int dataAdded = query.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_ADDED);
            final int _size = query.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
            do {
                final String imageId = query.getString(id);
                final String imagePath = query.getString(data);
                final long imageDate = query.getLong(dataAdded);
                final long size = query.getLong(_size);
                final ImageItem imageItem = new ImageItem();
                imageItem.type = 1;
                imageItem.imageId = imageId;
                imageItem.imagePath = imagePath;
                imageItem.imageDate = imageDate;
                imageItem.size = size;
                imageItem.thumbnailPath = thumbnail.get(imageId);
                list.add(imageItem);
            } while (query.moveToNext());
        }
        query.close();
        return list;
    }

    public static List<ImageItem> getImageListBybucketId(final Context context, String imageId) {
        final ArrayList<ImageItem> list = new ArrayList<ImageItem>();
        imageId = "bucket_id=" + imageId;
        final Cursor query =
                context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        new String[]{"_id", "bucket_id",
                                "_data"}, imageId, null, null);
        if (query == null) {
            Logc.e(AlbumHelper.TAG, "cursor is null");
            return list;
        }
        while (query.moveToNext()) {
            final int id = query.getColumnIndexOrThrow("_id");
            final int data = query.getColumnIndexOrThrow("_data");
            imageId = query.getString(id);
            final String imagePath = query.getString(data);
            final ImageItem imageItem = new ImageItem();
            imageItem.imageId = imageId;
            imageItem.imagePath = imagePath;
            list.add(imageItem);
        }
        query.close();
        return list;
    }

    public static List<ImageBucket> getImagesBucketList(final Context context) {
        final Map<String, ImageBucket> buildImagesBucketList = buildImagesBucketList(context);
        final ArrayList<ImageBucket> list = new ArrayList<ImageBucket>();
        for (Map.Entry<String, ImageBucket> stringImageBucketEntry :
                buildImagesBucketList.entrySet()) {
            list.add(stringImageBucketEntry.getValue());
        }
        return list;
    }

    private static Map<String, String> getThumbnail(final Context context) {
        final Cursor query =
                context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                        new String[]{"_id", "image_id",
                                "_data"}, null, null, null);
        if (query == null) {
            Logc.e(AlbumHelper.TAG, "cursor is null");
            return new HashMap<String, String>();
        }
        final Map<String, String> thumbnailColumnData = getThumbnailColumnData(query);
        query.close();
        return thumbnailColumnData;
    }

    private static Map<String, String> getThumbnailColumnData(final Cursor cursor) {
        final HashMap<String, String> hashMap = new HashMap<String, String>();
        if (cursor.moveToFirst()) {
            final int image_id = cursor.getColumnIndex("image_id");
            final int data = cursor.getColumnIndex("_data");
            do {
                hashMap.put(String.valueOf(cursor.getInt(image_id)), cursor.getString(data));
            } while (cursor.moveToNext());
        }
        return hashMap;
    }
}
